<template>
  <div>
    <!-- 添加或修改终端对话框 -->
    <el-dialog
      v-model="open"
      :close-on-click-modal="false"
      :title="title"
      width="500px"
      append-to-body
    >
      <el-form
        ref="editForm"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="编号" prop="clientId">
          <el-input v-model="form.clientId" placeholder="请输入编号" :disabled="!isAdd" />
        </el-form-item>
        <el-form-item v-if="isAdd" label="安全码" prop="originSecret">
          <el-input v-model="form.originSecret" placeholder="请输入安全码" />
        </el-form-item>
        <el-form-item v-else label="安全码" prop="clientSecret">
          <el-input v-model="form.clientSecret" placeholder="请输入安全码" />
        </el-form-item>
        <el-form-item label="授权范围" prop="scope">
          <el-input v-model="form.scope" placeholder="请输入授权范围" />
        </el-form-item>
        <el-form-item label="授权类型" prop="authorizedGrantTypes">
          <el-input v-model="form.authorizedGrantTypes" placeholder="请输入授权类型" />
        </el-form-item>
        <el-form-item label="令牌时效" prop="accessTokenValidity">
          <el-input-number v-model="form.accessTokenValidity" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="刷新时效" prop="refreshTokenValidity">
          <el-input-number v-model="form.refreshTokenValidity" controls-position="right" :min="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import { ref, getCurrentInstance, watch, defineExpose } from 'vue'
import { ElMessage } from 'element-plus'
import { getType, addType, updateType } from '@/api/system/dict'
import useDicts, { DictDirection } from '@/store/modules/dicts'
import { listClient, getClient, delClient, addClient, updateClient } from '@/api/system/client'

const dicts = useDicts()
// 表单参数
let form = ref({
    clientId: undefined,
    originSecret: undefined,
    clientSecret: undefined,
    scope: undefined,
    authorizedGrantTypes: undefined,
    accessTokenValidity: undefined,
    refreshTokenValidity: undefined,
  }),
  // 表单校验
  rules = {
    clientId: [{ required: true, message: '编号不能为空', trigger: 'blur' }],
    clientSecret: [{ required: true, message: '安全码不能为空', trigger: 'blur' }],
    scope: [{ required: true, message: '授权范围不能为空', trigger: 'blur' }],
    authorizedGrantTypes: [{ required: true, message: '授权类型不能为空', trigger: 'blur' }],
  },
  // 标题
  title = ref(''),
  // 模态框显示
  open = ref(false),
  isAdd = ref(false),
  // 新增修改表单
  editForm = ref(null),
  {
    appContext: {
      config: {
        globalProperties: { $resetForm, $validateForm },
      },
    },
  } = getCurrentInstance()

const emit = defineEmits(['changeList'])
// 重置新增修改列表
const reset = () => {
  form.value = {
    clientId: undefined,
    originSecret: undefined,
    clientSecret: undefined,
    scope: 'server',
    authorizedGrantTypes: 'password,refresh_token',
    accessTokenValidity: 3600,
    refreshTokenValidity: 7200,
  }
  $resetForm(editForm.value)
}
// 新增修改——确定
const submitForm = () => {
  $validateForm(editForm.value, () => {
    let msg = ''
    let response = null
    if (!isAdd.value && form.value.clientId != undefined) {
      msg = '修改成功'
      response = updateClient(form.value)
    } else {
      msg = '新增成功'
      response = addClient(form.value)
    }
    response
    .then((res) => {
      if (res.code === 200) {
        ElMessage.success(msg)
        open.value = false
        emit('changeList')
      } else {
        ElMessage.error(res.msg)
      }
    })
    .catch((error) => {
      ElMessage.error(error)
    })
  })
}
// 修改新增——取消
const cancel = () => {
  open.value = false
}
const getList = (id) => {
  getClient(id)
  .then((res) => {
    if (res.code === 200) {
      form.value = res.data
    } else {
      ElMessage.error(res.msg)
    }
  })
  .catch((err) => {
    console.log(err)
    ElMessage.error(err)
  })
}

defineExpose({
  open,
  isAdd,
  title,
  getList,
  form,
})
</script>
